package app.common.utils.cjt.vo;

import app.common.utils.NumberUtils;
import app.common.utils.cjt.ao.OtherDispatchCreateAo;
import app.common.utils.cjt.ao.ToOtherDispatchCreateAoDto;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author feizns
 * @since 2023/04/23
 */
@NoArgsConstructor
@Data
public class WmsStockoutProcessQueryWithDetailVo {

    /**
     * total_count
     */
    private Integer total_count;
    /**
     * order
     */
    private List<Order> order;

    /**
     * Order
     */
    @NoArgsConstructor
    @Data
    public static class Order implements ToOtherDispatchCreateAoDto {
        /**
         * logistics_name
         */
        private String logistics_name;
        /**
         * consign_time
         */
        private String consign_time;
        /**
         * error_info
         */
        private String error_info;
        /**
         * detail_list
         */
        private List<DetailList> detail_list;
        /**
         * goods_type_count
         */
        private Integer goods_type_count;
        /**
         * remark
         */
        private String remark;
        /**
         * goods_count
         */
        private Integer goods_count;
        /**
         * stockout_id
         */
        private Integer stockout_id;
        /**
         * warehouse_no
         */
        private String warehouse_no;
        /**
         * checked_goods_total_cost
         */
        private Double checked_goods_total_cost;
        /**
         * modified
         */
        private String modified;
        /**
         * note_count
         */
        private Integer note_count;
        /**
         * logistics_no
         */
        private String logistics_no;
        /**
         * created
         */
        private String created;
        /**
         * stockout_no
         */
        private String stockout_no;
        /**
         * src_order_type
         */
        private Integer src_order_type;
        /**
         * operator_name
         */
        private String operator_name;
        /**
         * weigh_post_cost
         */
        private Integer weigh_post_cost;
        /**
         * warehouse_name
         */
        private String warehouse_name;
        /**
         * checkouter_name
         */
        private String checkouter_name;
        /**
         * producer_name
         */
        private String producer_name;
        /**
         * process_no
         */
        private String process_no;
        /**
         * status
         */
        private Integer status;

        @Override
        public OtherDispatchCreateAo.Dto toOtherDispatchCreateAo() {
            OtherDispatchCreateAo.Dto dto = new OtherDispatchCreateAo.Dto();
            dto.setExternalCode(this.getStockout_no());
            dto.setCode(this.getStockout_no());
            dto.setVoucherDate(this.getConsign_time());
            dto.setVoucherType(new OtherDispatchCreateAo.Dto.VoucherType("ST1024"));
            dto.setBusiType(new OtherDispatchCreateAo.Dto.BusiType("13"));
            dto.setWarehouse(new OtherDispatchCreateAo.Dto.Warehouse(this.getWarehouse_no()));
            dto.setMemo("来源：[旺店通] 表单：[生产出库单] 编号：[" + this.getStockout_no() + "] 备注:[" + this.getRemark() + "]");
            dto.setRdStyle(new OtherDispatchCreateAo.Dto.RdStyle("211"));
            List<OtherDispatchCreateAo.Dto.RDRecordDetails> detailsList = this.detail_list.stream().map(DetailList::toRDRecordDetails).collect(Collectors.toList());
            dto.setRDRecordDetails(detailsList);
            return dto;
        }

        /**
         * DetailList
         */
        @NoArgsConstructor
        @Data
        public static class DetailList {
            /**
             * rec_id
             */
            private Integer rec_id;
            /**
             * spec_no
             */
            private String spec_no;
            /**
             * goods_no
             */
            private String goods_no;
            /**
             * goods_name
             */
            private String goods_name;
            /**
             * short_name
             */
            private String short_name;
            /**
             * spec_code
             */
            private String spec_code;
            /**
             * spec_name
             */
            private String spec_name;
            /**
             * spec_id
             */
            private Integer spec_id;
            /**
             * barcode
             */
            private String barcode;
            /**
             * num
             */
            private Double num;
            /**
             * scan_type
             */
            private Integer scan_type;
            /**
             * defect
             */
            private Boolean defect;
            /**
             * expire_date
             */
            private String expire_date;
            /**
             * remark
             */
            private String remark;
            /**
             * position_no
             */
            private String position_no;
            /**
             * batch_no
             */
            private String batch_no;
            /**
             * expect_num
             */
            private Integer expect_num;
            /**
             * checked_cost_price
             */
            private Double checked_cost_price;
            /**
             * brand_name
             */
            private String brand_name;
            /**
             * aux_unit_name
             */
            private String aux_unit_name;
            /**
             * base_unit_name
             */
            private String base_unit_name;

            public OtherDispatchCreateAo.Dto.RDRecordDetails toRDRecordDetails() {
                OtherDispatchCreateAo.Dto.RDRecordDetails details = new OtherDispatchCreateAo.Dto.RDRecordDetails();
                details.setInventory(new OtherDispatchCreateAo.Dto.RDRecordDetails.Inventory(this.getSpec_no()));
                details.setInvLocation(new OtherDispatchCreateAo.Dto.RDRecordDetails.InvLocation(this.getPosition_no()));
                details.setBaseQuantity(BigDecimal.valueOf(this.getNum()));
                details.setAmount(BigDecimal.valueOf(this.getChecked_cost_price()));
                if ( this.num != 0 )
                    details.setPrice(NumberUtils.fourDecimalPlaces(BigDecimal.valueOf(this.getChecked_cost_price() / this.getNum())));
                return details;
            }
        }
    }
}
